<?php


namespace App\Admin\Controllers;


use App\Admin\Actions\Grid\ShowHistoryByRoom;
use App\Admin\Repositories\Owner;
use App\Models\Room;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Illuminate\Support\Facades\DB;

class HistoryRentController extends AdminController
{
    protected $title = '房间租借历史';

    protected function grid()
    {
        // 去重复
        $arr = \App\Models\Owner::query()->get()->groupBy('ownerable_type')->toArray();

        return Grid::make(
            new Owner(['ownerable']),
            function (Grid $grid) use ($arr) {
                $grid->disableViewButton();
                $grid->disableEditButton();
                $grid->model()
                    ->select(DB::raw('distinct ownerable_id'))
                    ->where('ownerable_type', Room::class)
                    ->whereIn('ownerable_id', array_column($arr[Room::class], 'ownerable_id'));

                $grid->disableDeleteButton();
                $grid->disableBatchDelete();
                $grid->column('ownerable', '租借房间名称')->display(
                    function () {
                        $room = Room::query()->with('building.house')->where('id', $this->ownerable_id)->first();

                        return $room->building->house->name.' - '.$room->building->name.' - '.$room->name;
                    }
                );
                $grid->actions(new ShowHistoryByRoom());
            }
        );
    }

   public function show($id, Content $content)
   {
       return $content->header('房间租借历史')
           ->description('详情')
           ->body(Show::make($id, new Room(['owner']), function (Show $show) {
               $show->id('ID');
               $show->name('房间名称');
           }));
   }
}